package com.reactim.message.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.util.UUID;

/**
 * 实例配置类
 * 提供服务实例相关的配置信息
 */
@Configuration
public class InstanceConfig {
    
    @Value("${spring.application.name:reactim-message-service}")
    private String applicationName;
    
    @Value("${server.port:8080}")
    private int serverPort;
    
    /**
     * 生成唯一的实例ID
     * 格式: applicationName-hostname-port-uuid
     */
    @Bean
    public String instanceId() {
        try {
            String hostname = InetAddress.getLocalHost().getHostName();
            String shortUuid = UUID.randomUUID().toString().substring(0, 8);
            return String.format("%s-%s-%d-%s", applicationName, hostname, serverPort, shortUuid);
        } catch (Exception e) {
            // 如果获取主机名失败，使用随机UUID
            String shortUuid = UUID.randomUUID().toString().substring(0, 8);
            return String.format("%s-unknown-%d-%s", applicationName, serverPort, shortUuid);
        }
    }
}